# -=- encoding: utf-8

from django.db import models
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic

from adl.base.models import Usercreated

class UserProfile(models.Model):
    "Zusätzliche Benutzerdaten"
    user = models.OneToOneField(User)
    activation_key = models.CharField(max_length=40)
    key_expires = models.DateTimeField(null = True)
    
    def __unicode__(self):
        return u"%s (%s)" % (self.user.username, self.main.name)

    def _main(self):
        return self.character_set.filter(is_main = True)
    main   = property(fget = _main)
    
    def _twinks(self):
        return self.character_set.filter(is_main = False)
    twinks = property(fget = _twinks)
    
    class Meta:
        ordering = ['user']
        verbose_name = u"Benutzer"
        verbose_name_plural = u"Benutzerprofil"

class Tag(models.Model):
    "Ein Tag für beliebige Models"
    slug          = models.SlugField           (verbose_name=u"Tagname", help_text=u"Name des Tags, wird automitisch als slug behandelt.")
    num_used      = models.PositiveIntegerField(verbose_name=u"Anzahl",  help_text=u"Anzahl der Objekte, die mit diesem Tag getaggt wurden. Der Wert wird gespeichert, um unnötige Datenbankoperationen zu vermeiden.")
    
    def __unicode__(self):
        return u"%s" % self.name
    
    class Meta:
        verbose_name = "Tag"
        verbose_name_plural = "Tags"    
    
    def inc_count(self, inc=True):
        ## for DEV
        # return 0 # TODO include count in db-model
    
        ## functionality
        if inc:
            self.num_used += 1
        else:
            self.num_used -= 1
        self.save()
        return self.num_used
    
    def _count_tagged_objects(self):
        return self.taggings.count()
    count = property(fget = _count_tagged_objects)
    
    
class Tagging(models.Model):
    "Beziehung zwischen Tag und getaggtem Objekt"
    tag            = models.ForeignKey(Tag, related_name="taggings")
    content_type   = models.ForeignKey(ContentType)
    object_id      = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')
    
    class Meta:
        ordering = ["tag"]
        verbose_name = u"Tagging"
        verbose_name_plural = u"Taggings"    
    
    def __unicode__(self):
        return u"%s" % self.tag.name
    
class Attachment(Usercreated):
    "Dateianhang"
    file       = models.FileField    (upload_to = "attachments/%Y/%m/%d", verbose_name = "Datei",        help_text = "Pfad zur Datei", max_length=255 )
    
    def __unicode__(self):
        return u"%s" % self.file.name
    
    class Meta:
        verbose_name = u"Anhang"
        verbose_name_plural = u"Anhänge"    
    
class Entry(Usercreated):
    "Eintrag mit Text"
    text       = models.TextField    (verbose_name = u"Inhalt")
    
    def __unicode__(self):
        return u"%s" % self.text[:200]
    
    class Meta:
        verbose_name = u"Eintrag"
        verbose_name_plural = u"Einträge"            